package com.jht.bletool2java.activity.blepage;


import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.github.rahatarmanahmed.cpv.CircularProgressView;
import com.google.android.material.tabs.TabLayout;
import com.hjq.xtoast.XToast;
import com.jht.bletool2java.BLEApplication;
import com.jht.bletool2java.R;
import com.jht.bletool2java.activity.BaseActivity;
import com.jht.bletool2java.activity.blepage.adapter.BLEPageAdapter;
import com.jht.bletool2java.activity.blepage.fragment.blecontrol.BLEControlFragment;
import com.jht.bletool2java.activity.blepage.fragment.log.BleLogFragment;
import com.jht.bletool2java.activity.homepage.fragment.bledevice.adapter.BleDeviceAdapter;
import com.jht.bletool2java.activity.homepage.fragment.bledevice.bean.BLEDevice;

import java.util.ArrayList;
import java.util.List;

public class BleDataActivity extends BaseActivity implements IBleData.UI {
    private static final String TAG = "BleDataActivity";
    private TabLayout mTabLayout2;
    private ViewPager mVpDataTransmission;
    private ImageView mIvBack;
    private TextView mTvTitle;
    private CircularProgressView mProgressView;
    private ImageView mIvClearLog;
    private ImageView mIvRecordData;
    private ImageView mIvQuestion;
    private ImageView mIvMenu;
    private TextView mTvTitle2;


    private BLEDevice ble_device;
    private BLEControlFragment bleServiceFragment;
    private BleLogFragment bleLogFragment;
    private XToast show;
    private XToast showClear_log;
    private XToast execRecord_data;
    private byte[] scan_record;

    @Override
    public int getLayoutID() {
        return R.layout.activity_ble_data;
    }

    @Override
    public void initViewAndPresenter() {
        mTvTitle2 = findViewById(R.id.tv_title2);
        mIvMenu = findViewById(R.id.iv_menu);
        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mProgressView = findViewById(R.id.progress_view);
        mTabLayout2 = findViewById(R.id.tab_layout_2);
        mVpDataTransmission = findViewById(R.id.vp_data_transmission);

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bleServiceFragment != null) {
                    bleServiceFragment.clear();
                }
                finish();
            }
        });
        mIvBack.setVisibility(View.VISIBLE);
        mIvMenu.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.GONE);
        mTvTitle2.setVisibility(View.VISIBLE);
        mProgressView.setVisibility(View.GONE);
        mVpDataTransmission.setOffscreenPageLimit(2);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        boolean containBTDevice = initActivityData(intent);
        if (!containBTDevice) {
            finish();
            return;
        }
        initViewPage();
    }

    private boolean initActivityData(Intent intent) {
        if (intent == null) return false;
        ble_device = (BLEDevice) (intent.getParcelableExtra(BleDeviceAdapter.BLE_DEVICE));
        if (ble_device != null) {
            scan_record = ble_device.getScanRecord();
            mTvTitle2.setText(ble_device.getDevice().getName());
            return true;
        } else {
            //传过来的蓝牙设备是null
            return false;
        }
    }

    private void initViewPage() {
        List<Fragment> fragments = initFragments();
        BLEPageAdapter myViewPageAdapter = new BLEPageAdapter(getSupportFragmentManager(), fragments, initFragmentHeader());
        mVpDataTransmission.setAdapter(myViewPageAdapter);
        mTabLayout2.setupWithViewPager(mVpDataTransmission);
    }

    private List<Fragment> initFragments() {
        bleLogFragment = BleLogFragment.newInstance(ble_device);
        bleServiceFragment = BLEControlFragment.newInstance(ble_device);
        List<Fragment> data = new ArrayList<>();
        data.add(bleServiceFragment);
        data.add(bleLogFragment);
        return data;
    }

    private String[] initFragmentHeader() {
        String[] header_str = new String[2];
        Resources resources = BLEApplication.getBLEApplicationContext().getResources();
        header_str[0] = resources.getString(R.string.bt_service);
        header_str[1] = resources.getString(R.string.real_time_log);
        return header_str;
    }

    @Override
    public void showToast(int msgId) {
        String msg = getResources().getString(msgId);
        Toast.makeText(BLEApplication.getBLEApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }



}